In this Tutorials section we cover and explain Creating Url in details for freshers and experienced
previous | Home | Next |
To Creating URL
The easy way to creating a URL object or instance is from a String. and it's represents the human-readable. The human -readable form of the URL address. This is typically the form that another person will use for a URL. Java program in using , you can create a URL object or instances to using a String containing.
URL myURL = new URL("http://example.com/");
The URL object or instances created and his above represents an absolute URL. This absolute URL contains to all of the information necessary, and this URL is to reach the resource in question. You can also create URL objects from a relative URL address.
To Parsing URL
The URL class providing several methods that let you query URL objects or instances. You can get the protocol, authority, host name, port number, path, query, filename, and reference from a URL using these accessor methods
getProtocolReturns the protocol identifier component of the URL.
getAuthorityReturns the authority component of the URL.
getHostReturns the host name component of the URL.
getPortReturns the port number component of the URL. The getPort method returns an integer that is the port number. If the port is not set, getPort returns -1.
getPathReturns the path component of this URL.
getQueryReturns the query component of this URL.
getFileReturns the filename component of the URL. The getFile method returns the same as getPath, plus the concatenation of the value of getQuery, if any.
getRefReturns the reference component of the URL.
Note:Remember that not all URL addresses contain these components. The URL class provides these methods because HTTP URLs do contain these components and are perhaps the most commonly used URLs. The URL class is somewhat HTTP-centric.
You can use these getXXX methods to get information about the URL regardless of the constructor that you used to create the URL object.
The URL class, along with these accessor methods, frees you from ever having to parse URLs again! Given any string specification of a URL, just create a new URL object and call any of the accessor methods for the information you need. This small example program creates a URL from a string specification and then uses the URL object's accessor methods to parse the URL
Example
import java.net.*; import java.io.*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost()); System.out.println("port = " + aURL.getPort()); System.out.println("path = " + aURL.getPath()); System.out.println("query = " + aURL.getQuery()); System.out.println("filename = " + aURL.getFile()); System.out.println("ref = " + aURL.getRef()); } } Here's the output displayed by the program: protocol = http authority = example.com:80 host = example.com port = 80 path = /docs/books/tutorial/index.html query = name=networking filename = /docs/books/tutorial/index.html?name=networking ref = DOWNLOADING
previous | Home | Next |